package com.pic.mgt.haishin.initials;

import java.util.List;

import com.pic.mgt.initials.AbstractTerminalMonitorInitialBean;
import com.pic.mgt.initials.InitialOperationException;
import com.pic.mgt.log.IgxLog;
import com.pic.mgt.manager.terminal.BasicTerminalInfoBean;
import com.pic.mgt.manager.terminal.TerminalManager;
import com.pic.mgt.monitor.SimpleUnitMonitor;
import com.pic.mgt.monitor.UnitMonitor;
import com.pic.mgt.monitor.UnitMonitorException;

/**
 * 做配信訊息目錄監控初始化。
 * @author p10118178
 *
 */
public final class HaishinTerminalMonitorInitialBean extends
		AbstractTerminalMonitorInitialBean {
	private UnitMonitor unitMonitor;
	private TerminalManager terminalManager;
	
	@Override
	public void initialize() throws InitialOperationException {
		// 取得有效端末
		List<BasicTerminalInfoBean> effectiveBasicTerminalInfoBeanList =
			this.terminalManager.getEffectiveBasicTerminalInfoBeanList();
		
		for (BasicTerminalInfoBean effectiveBasicTerminalInfoBean :
			                       effectiveBasicTerminalInfoBeanList) {
			String terminalMonitorPathStr = 
				SimpleUnitMonitor.getTerminalHaishinOutboxPathStr(
						effectiveBasicTerminalInfoBean.getTerminalId());
			try {
				this.unitMonitor.addMonitorPath(terminalMonitorPathStr);
			} catch (UnitMonitorException e) {
				try {
					throw new InitialOperationException("" +
							"SUDA-229 Initializing failed: adding terminal folder '" +
							terminalMonitorPathStr + "' monitor error." ,e);
				} catch (InitialOperationException e1) {
					// ********************** Log **********************
					IgxLog.messageLog.error(this, e1.getMessage());
					// ********************** Log **********************
					
					throw e1;
				}
			}
		}
		
		// ********************** Log **********************
		if (IgxLog.messageLog.isDebugMode()) {
			IgxLog.messageLog.debug(this,
					"Initializing terminal folder monitor completed.");
		}
		// ********************** Log **********************
	}
	
	//	--- getter and setter ---
	
	public void setUnitMonitor(UnitMonitor unitMonitor) {
		this.unitMonitor = unitMonitor;
	}

	public void setTerminalManager(TerminalManager terminalManager) {
		this.terminalManager = terminalManager;
	}
}
